﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web.UI;
using WinnerCMS.BLL;
using WinnerCMS.BLL.PageFactory;
using WinnerCMS.BLL.PageFactory.TagModule;
using WinnerCMS.Common;
using WinnerCMS.Model;

namespace WinnerCMS.WebFactory
{
    public class SoftShow : PublicPage<Soft>
    {
        #region 软件内容
        private void SoftContext(StringBuilder tempStrBuilder)
        {
            tempStrBuilder.Replace("[ContentId]", this.Model.Id.ToString());


            string strTemp = TagEngine.GetString(tempStrBuilder.ToString(), "DownloadUrl", GetWhich.All);
            if (this.Model.Rank == 0)
            {
                if (string.IsNullOrEmpty(strTemp) == false)
                {
                    tempStrBuilder.Replace(strTemp, Downloads(strTemp, this.Model.Id));
                }
                tempStrBuilder.Replace("@DownLoadPurview@", "非会员软件");
            }
            else
            {
                if (string.IsNullOrEmpty(strTemp) == false)
                {
                    tempStrBuilder.Replace(strTemp, Downloads(strTemp, this.Model.Id));
                }

                tempStrBuilder.Replace("@DownLoadPurview@", "会员软件，下载扣除点数：" + this.Model.PointNum);
            }

            PageCommon.RepalceFiled(tempStrBuilder, this.Model);
        }

        #region 下载地址

        /// <summary>
        /// 下载地址
        /// </summary>
        /// <param name="downloadUrlString"></param>
        /// <param name="softId"></param>
        /// <returns></returns>
        static public string Downloads(string downloadUrlString, int softId)
        {
            IList<SoftDownList> list = new BLL.SoftDownListManage().GetDownList(1, softId);
            StringBuilder strDownLoadUrl = new StringBuilder();
            string item = TagEngine.GetString(downloadUrlString, "DownloadUrl", GetWhich.ItemTemplate);
            for (int i = 0; i < list.Count; i++)//循环N行
            {
                strDownLoadUrl.Append(item);

                strDownLoadUrl.Replace("@SoftID@", list[i].SoftId.ToString());
                strDownLoadUrl.Replace("@ID@", list[i].Id.ToString());
                strDownLoadUrl.Replace("@DownName@", list[i].ServerHTML);
            }
            return strDownLoadUrl.ToString();
        }
        #endregion
        #endregion

        #region 重写Render
        protected override void Render(HtmlTextWriter writer)
        {
            StringBuilder winner = new StringBuilder();
            using (TextWriter tempWriter = new StringWriter())
            {
                base.Render(new HtmlTextWriter(tempWriter));
                winner.Append(tempWriter.ToString());
            }

            #region 通用模型标签
            IList<string> matchresult = TagEngine.TagCollection(winner.ToString(), "Slide");
            foreach (string nextmatch in matchresult)
            {
                winner.Replace(nextmatch, PageCommon.Slide(nextmatch));
            }


            #endregion

            SoftContext(winner);


            matchresult = TagEngine.TagCollection(winner.ToString(), "CommentShow");
            foreach (string nextmatch in matchresult)
            {
                if (this.Model.AllowComment )
                {
                    winner.Replace(nextmatch, CommentCommon.CommentShow(nextmatch, this.Model.Id));
                }
                else
                {
                    winner.Replace(nextmatch, string.Empty);
                }
            }


            CategoryCommon.Path(winner, this.Model.CategoryId);

            if (Request["Method"] != null)
            {
                string path = StaticFilePath.ContentShowUrl(this.Model);
                new FileIO().WriterFile(HttpServer.MapPath(path), winner.ToString());

                writer.Write(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff"));
            }
            else
            {
                writer.Write(winner.ToString());
            }
        }
        #endregion

    }
}